home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 034a / trunclog.zip / TRUNCLOG.PAS < prev   
Pascal/Delphi Source File  |  1991-01-13  |  3KB  |  88 lines

  1. Program TruncLog; {FrontDoor LOG-file truncator}
  2.         {SYNTAX: trunclog <logfile> <days to keep (max 255)> [<keepfile>]}
  3.         {Written by Erik Wachtmeester, 10-jan-1991 in Turbo Pascal (tm) 5.5}
  4. uses Dos;
  5.  
  6. type DateStr = string[9];
  7.      Str127 = string[127];
  8.  
  9. const MonthName: array [1..12] of string[3] =
  10.                  ('Jan','Feb','Mar','Apr','May','Jun',
  11.                   'Jul','Aug','Sep','Oct','Nov','Dec');
  12.  
  13. var DateNr: longint;
  14.     Year, Month, Day, DOW: word;
  15.     KeepDays: byte;
  16.     Dummy: integer;
  17.     LogName, KeepName, Line: string[80];
  18.     KeepLog, Keep: boolean;
  19.     Infile, Outfile, Keepfile: text;
  20.  
  21. function DateConv(Nyear,Nmonth,Nday: word): longint;
  22. begin
  23.   DateConv := trunc(365.25*Nyear)+trunc(30.6*Nmonth)+Nday
  24.               -(Nyear div 100)+(Nyear div 400)-723241;    {01-jan-1980 = 1}
  25. end;
  26.  
  27. function GetDNum(Line: string): longint;
  28. var Gyear, Gmonth, Gday: word;
  29.     I: byte;
  30.     CLine: ^Str127;
  31. begin
  32.   CLine := Ptr(PrefixSeg, $80);       { "abuse" TP's parameter parsing to }
  33.   CLine^ := Line;                     { extract the date from the log entry. }
  34.   val(copy(ParamStr(5),1,2),Gyear,Dummy);
  35.   Gyear := Gyear+1900;
  36.   Gmonth := 0;
  37.   for I := 1 to 12 do
  38.     if ParamStr(4)=MonthName[I] then Gmonth := I;
  39.   val(ParamStr(3),Gday,Dummy);
  40.   GetDNum := DateConv(Gyear,Gmonth,Gday);
  41. end;
  42.  
  43. begin
  44.   writeln;
  45.   writeln('*** TRUNCLOG FrontDoor log file truncator, by Erik Wachtmeester ***');
  46.   if length(ParamStr(1))>0 then LogName := ParamStr(1);
  47.   if length(ParamStr(2))>0 then val(ParamStr(2),KeepDays,Dummy);
  48.   Keep := False;
  49.   if length(ParamStr(3))>0 then begin
  50.     KeepName := ParamStr(3);
  51.     assign(KeepFile,KeepName);
  52.     {$I-} reset(KeepFile); {$I+}
  53.     if ioresult=0 then begin
  54.       close(KeepFile);
  55.       append(KeepFile);
  56.     end else rewrite(KeepFile);
  57.     Keep := True;
  58.   end;
  59.   assign(Infile,LogName);
  60.   {$I-} reset(Infile); {$I+}
  61.   if ioresult+Dummy<>0 then begin
  62.     writeln;
  63.     writeln(^G,'*** TRUNCLOG error! ***');
  64.     writeln('    Syntax: TRUNCLOG <logfile> <days to keep (max. 255)> [<keepfile>]');
  65.     writeln('            <logfile> will be truncated as of today minus <days to keep>,');
  66.     writeln('            and all older lines will be written to <keepfile> (optional).');
  67.     writeln;
  68.     halt(1);
  69.   end;
  70.   GetDate(Year,Month,Day,DOW);
  71.   DateNr := DateConv(Year,Month,Day)-KeepDays;
  72.   KeepLog := False;
  73.   assign(Outfile,'TRUNCLOG.$$$');
  74.   rewrite(Outfile);
  75.   while not eof(Infile) do begin
  76.   readln(Infile,Line);
  77.     if not KeepLog then
  78.       if copy(Line,1,10)='----------' then
  79.         if GetDNum(Line)>=DateNr then KeepLog := True;
  80.     if KeepLog then writeln(Outfile,Line)
  81.     else if Keep then writeln(KeepFile,Line);
  82.   end;
  83.   close(Infile);
  84.   close(Outfile);
  85.   erase(Infile);
  86.   rename(Outfile,LogName);
  87.   if Keep then close(KeepFile);
  88. end.